<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/15
 * Time: 23:05
 */
namespace app\common\logic\export;





use app\common\logic\ExportService;
use app\common\model\shop\Order;
use app\common\utils\Utils;
use think\Db;
use think\exception\ValidateException;

class NiuOrderExport
{
    //导出订单
    private static $new; //申请一个私有的静态成员变量来保存该类的唯一实例
    const TITLE_ARR = [
        'ID'         => [1, 1, 9],  //0为开始  1为结束
        '牛的名称'                 => [2,2, 12],
        '姓名'                       =>[3,3, 12],
        '电话'        => [4,4, 10],
        '牧场名称'                              => [5,5, 12] ,
        '订购数量'                   =>[6,6, 9],
        '成交金额'                          =>[7,7, 10],
        '处理进度'                          =>[8,8, 10],
        '地址'                          =>[9,9, 20],
        '订购时间'                          =>[10,10, 18],
        '使用时间'                          =>[11,11, 18],
        '备注'                          =>[12,12, 10],
    ];
    const SUBMIT = [
        'author' =>'育牛',
        'subject' =>'育牛',
        'title' =>'订单导出',
    ];
    private function __construct() {

    }


    public static function instance () { //声明一个静态公共方法，供外部获取唯一实例
        if (!(self::$new instanceof self)) {
            self::$new = new self;
        }
        return self::$new;
    }




    /**
     * 生成税率 报表
     */
    public function export($where = []) {
        $name = '订单导出';
        $titleArr = self::TITLE_ARR;
        $submit = self::SUBMIT;

        //list($sheetName, $sort, $line, $sheet) = [ $timeStr . '-表0', 1, 1, 0]; //初始化变量
        list($sheetName, $sort, $line, $sheet) = [$name, 1, 1, 0]; //初始化变量
        $excel = ExportService::instance()->setTitles($submit);

        //初始化
        $excel->createSheet($sheetName, $sheet)->setSheetHeader($line, $titleArr);
        $list = (new Order())->with(['user', 'goods'])->where($where)->order('num', 'desc')->select();


        foreach ($list as $k => $v) {
            $line ++;
            $good = Db::name('shop_goods')->where('id', $v['goods_id'])->find();
            $area = Db::name('area')->where('id', 'in', [$v['province'], $v['city'], $v['area']])->order('id', 'asc')->column('name');
            $str = Utils::implodeArr($area, '_') . '_'. $v['address'];


            $value = [
                $v['id'],
                $good['name'],
                $v['name'],
                $v['mobile'],
                $v['company_text'],
                $v['num'] ,
                $v['money'] ,
                (new Order())->getTypeTextAttr($v['type'], $v),
                $str,
                Utils::time_format($v['createtime']),
                $v['use_time'] > 0 ? Utils::time_format($v['use_time']) : "无使用时间",
                $v['remark'],

            ];
            $excel->insertValues($line, $value);
            $sort ++;
        }

        $excel->output('Xlsx', $name);
        return true;
    }


    private function __clone() {} //声明私有的克隆方法，防止对象被克隆

    public function __sleep() { //重写__sleep方法，将返回置空，防止序列化反序列化获得新的对象
        return [];
    }
}